package com.android.aishangauction.fragment.member;


import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.aishangauction.R;
import com.android.aishangauction.adapt.BaseFragmentAdapter;
import com.android.aishangauction.fragment.BaseLazyFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 趣豆明细页
 */
public class Fragment_Mine_Coin_Detail extends BaseLazyFragment {

    @BindView(R.id.tv_tab_coin_income)
    TextView tv_Income;
    @BindView(R.id.line_tab_coin_income)
    View line_Income;
    @BindView(R.id.tv_tab_coin_expenditure)
    TextView tv_Expenditure;
    @BindView(R.id.line_tab_coin_expenditure)
    View line_Expenditure;
    @BindView(R.id.viewpager)
    ViewPager mViewPager;
    @BindView(R.id.free_coin)
    TextView tv_FreeCoin;
    @BindView(R.id.coin)
    TextView tv_Coin;
    @BindView(R.id.total_coin)
    TextView tv_TotalCoin;

    private FragmentActivity mcontext;
    private View view;
    private String explain;
    AlertDialog dialog;
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mFreeCoin;
    private String mCoin;

    public Fragment_Mine_Coin_Detail() {

    }

    public static Fragment_Mine_Coin_Detail newInstance(String coin, String free_coin) {
        Fragment_Mine_Coin_Detail fragment = new Fragment_Mine_Coin_Detail();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, coin);
        args.putString(ARG_PARAM2, free_coin);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mCoin = getArguments().getString(ARG_PARAM1);
            mFreeCoin = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected void initPrepare() {

    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {
//        getScoreData();
        setupViewPager();
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine_coin_detail, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        tv_Coin.setText(mCoin);
        tv_FreeCoin.setText(mFreeCoin);
        int totalCoin = Integer.parseInt(mCoin) + Integer.parseInt(mFreeCoin);
        tv_TotalCoin.setText(totalCoin+"");
        return view;
    }


//    public void getScoreData() {
//        String address = ConstantsUrl.domain + ConstantsUrl.MEMBER_COIN;
//        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
//        HashMap<String, String> map = new HashMap<>();
//        map.put("page", "0");
//        if (networkConnected) {
//            HashMap<String, String> header = ObjectUtils.getHeader(mcontext);
//            HttpUtils.get(address, header, map, dataCallback);
//        }
//    }
//
//    ScoreDetailDataCallback dataCallback = new ScoreDetailDataCallback() {
//
//        @Override
//        public void onError(Call call, Exception e, int id) {
//        }
//
//        @Override
//        public void onResponse(ScoreDetailData response, int id) {
//            if (response.getCode() == 0) {
//                explain = response.getExplain();
//            }
//        }
//    };

    private void setupViewPager() {

        ArrayList<BaseLazyFragment> mFragments = new ArrayList<>();
        Fragment_Mine_Coin_Detail_Tab fragment1 = Fragment_Mine_Coin_Detail_Tab.newInstance("1");
        Fragment_Mine_Coin_Detail_Tab fragment2 = Fragment_Mine_Coin_Detail_Tab.newInstance("2");
        mFragments.add(fragment1);
        mFragments.add(fragment2);

        BaseFragmentAdapter adapter =
                new BaseFragmentAdapter(getActivity().getSupportFragmentManager(), mFragments);

        mViewPager.setAdapter(adapter);
    }

    @OnClick({
            R.id.rl_tab_coin_expenditure, R.id.rl_tab_coin_income
    })
    public void onClick(View view) {
        switch (view.getId()) {
//            //积分说明
//            case R.id.score_detail_explain:
//                if (explain != null && !explain.equals("")) {
//                    showDialog();
//                }
//                break;
            //支出明细
            case R.id.rl_tab_coin_expenditure:
                tv_Expenditure.setTextColor(Color.RED);
                tv_Income.setTextColor(Color.BLACK);
                line_Expenditure.setVisibility(View.VISIBLE);
                line_Income.setVisibility(View.INVISIBLE);
                mViewPager.setCurrentItem(0);
                break;
            //收益明细
            case R.id.rl_tab_coin_income:
                tv_Income.setTextColor(Color.RED);
                tv_Expenditure.setTextColor(Color.BLACK);
                line_Income.setVisibility(View.VISIBLE);
                line_Expenditure.setVisibility(View.INVISIBLE);
                mViewPager.setCurrentItem(1);
                break;
        }
    }

    private void showDialog() {
        LayoutInflater inflater = (LayoutInflater) mcontext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.dialog_score_explain, null);
        TextView sure = (TextView) v.findViewById(R.id.dialog_sure);
        TextView content = (TextView) v.findViewById(R.id.dialog_content);
        content.setText(Html.fromHtml(explain));
        sure.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        dialog.dismiss();
                                    }
                                }
        );
        dialog = new AlertDialog.Builder(mcontext).setView(v).setCancelable(true).show();
    }
}
